﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace docmgr.WinForm
{
    public partial class DocumentDetail : Form
    {
        public int DocID;
        public DocumentDetail()
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));
        }
        public DocumentDetail(string key)
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));
            this.DocID = Convert.ToInt32(key);
        }

        private void DocumentDetail_Load(object sender, EventArgs e)
        {
           if(1!= Convert.ToInt32(Common.curUser.Rows[0]["PositionID"].ToString()))
           {
               button2.Visible = false;
           }
            init();
        }
        void init()
        {
            string sqlstr = String.Format("SELECT	doc.DocName 文件名,	dh.Code 编码,	u.name 持有人,	dh.Status 状态 ,dh.SysSerialNumber 卷号 FROM	DocHandler AS dh LEFT JOIN Document AS doc ON dh.DocID = doc.ID LEFT JOIN [User] AS u ON dh.HandlerUserID = u.id WHERE	doc.id ={0} ", this.DocID);

            dataGridView1.DataSource = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlstr).Tables[0];


            sqlstr = String.Format("select count(*) c from [DocConcerned] where DocID ={0} and UserID = {1}", this.DocID, Convert.ToInt32(Common.curUser.Rows[0]["ID"]));

            DataTable dt = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlstr).Tables[0];

            if(Convert.ToInt32(dt.Rows[0]["c"])==0)
            {
                button1.Text = "添加收藏";
            }
            else
            {
                button1.Text = "取消收藏";
            }
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                //TODO:
                if (Convert.ToString(Common.curUser.Rows[0]["UserName"]) == "everyhook" ||
                    Convert.ToString(Common.curUser.Rows[0]["UserName"]) == "fangz")
                {/**
                  * 用来进行调试的。
                  * 躺着赚钱实在是太舒服了
                  * 没有什么压力
                  * 调试用的，过会删掉
                  */
                    string key = dataGridView1.Rows[e.RowIndex].Cells["编码"].Value.ToString();
                    PopWindow frm = new PopWindow();
                    frm.code = key;
                    frm.ShowDialog();

                    init();
                }

                else
                    return;
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String sqlStr = "";
            if(button1.Text=="添加收藏")
            {
                sqlStr = String.Format("insert [DocConcerned] (DocID,UserID) values ({0},{1})", this.DocID, Convert.ToInt32(Common.curUser.Rows[0]["ID"]));
                button1.Text = "取消收藏";
            }
            else
            {
                sqlStr = String.Format("delete [DocConcerned] where DocID ={0} and UserID = {1}", this.DocID, Convert.ToInt32(Common.curUser.Rows[0]["ID"]));
                button1.Text = "添加收藏";
            }

            SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            /**
             * DocReadLog删除
             * DocConcerned删除
             * DocHandler删除
             * Document删除
             */

            String sqlStr1 = String.Format("delete DocReadLog where ID in( select ID from DocReadLog WHERE DocumentID= {0})",this.DocID);
            String sqlStr2 = String.Format("delete DocConcerned where ID in( select ID from DocConcerned WHERE DocID= {0})", this.DocID);
            String sqlStr3 = String.Format("delete DocHandler where ID in( select ID from DocHandler WHERE DocID= {0})", this.DocID);
            String sqlStr4 = String.Format("delete Document where ID ={0}", this.DocID);

            SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr1);
            SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr2);
            SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr3);
            SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr4);

            MessageBox.Show("删除成功！");
            this.Close();
        }
    }
}
